﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using elibrary.Classes;
using elibrary.Models.Managers;

namespace elibrary.Models.Entities
{
    public class Payment
    {
        /* Class Fields [Start] ----------------------------------------------------------------- */
        [Key]
        public int id { get; set; }
        public int orderId { get; set; }
        public DateTime paymentDate { get; set; }
        public string transCode { get; set; }
        public string payeeName { get; set; }
        public decimal amountPaid { get; set; }
        public PaymentStatus status { get; set; }
        /* Class Fields [End] ------------------------------------------------------------------- */

        /* Computed Fields [Start] -------------------------------------------------------------- */
        public UserOrder order
        {
            get { return this.getAssociatedOrder(this.orderId); }
            set { this.orderId = value.id; }
        }

        public bool isRejected
        {
            get { return this.status == PaymentStatus.Rejected; }
        }

        public bool isPending
        {
            get { return this.status == PaymentStatus.Pending; }
        }

        public bool isApproved
        {
            get { return this.status == PaymentStatus.Approved; }
        }
        /* Computed Fields [End] ---------------------------------------------------------------- */

        /* Class Enums [Start] ------------------------------------------------------------------ */
        public enum PaymentStatus { Pending = 0, Rejected, Approved }
        /* Class Enums [End] -------------------------------------------------------------------- */

        public bool rejectThis(bool autoReject = false)
        {
            if (this.status != PaymentStatus.Pending || this.order == null)
                return false;

            this.status = PaymentStatus.Rejected;
            Notice.notifyPaymentRejected(this.order.buyerId,this.id);

            if (autoReject)
                return this.order.cancelThis();

            return true;
        }

        public bool approveThis(bool autoComplete = true)
        {
            if (this.status != PaymentStatus.Pending || this.order == null)
                return false;

            this.status = PaymentStatus.Approved;

            if (autoComplete)
                return this.order.completeThis();

            return true;
        }

        // ensure only 1 pending/approved payment at any time
        public bool onAdding()
        {
            if (this.order == null)
                return false;

            PaymentManager paymentManager = new PaymentManager(AppGlobals.dataContext);
            List<Payment> payments = paymentManager.getAllByOrder(this.orderId);
            
            bool test = (this.status == PaymentStatus.Pending && payments.SingleOrDefault(
                q => q.status == PaymentStatus.Pending) != null) || 
                (this.status == PaymentStatus.Approved && payments.SingleOrDefault(
                q => q.status == PaymentStatus.Approved) != null);

            return !test;
        }

        private UserOrder getAssociatedOrder(int uid)
        {
            return null;
        }
    }
}